package com.thd.wei.service;

import com.thd.wei.entity.AlarmParamsDTO;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author : zhangqiang
 * @date : 2020/12/3 17:11
 */

@Service
@Slf4j
public class PushMessageServiceImpl implements PushMessageService{

    /**
     * 公众号appid
     */
    @Value("${wx.appid}")
    private  String appid;

    /**
     * 公众号appSecret
     */
    @Value("${wx.secret}")
    private  String secret;

    /**
     * 给微信公众号某个用户推送信息
     * @param alarmParamsDTO
     */
    @Override
    public void pushMessage(AlarmParamsDTO alarmParamsDTO) {
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appid);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        List<WxMpTemplateData> wxMpTemplateData = Arrays.asList(
                new WxMpTemplateData("first",alarmParamsDTO.getFirst(),"#000000"),
                new WxMpTemplateData("keyword1",alarmParamsDTO.getKeyword1(),"#000080"),
                new WxMpTemplateData("keyword2",alarmParamsDTO.getKeyword2(),"#0000FF"),
                new WxMpTemplateData("keyword3",alarmParamsDTO.getKeyword3(),"#FFD700"),
                new WxMpTemplateData("remark",alarmParamsDTO.getRemark(),"#00FF00")
        );
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(alarmParamsDTO.getOpenId())
                .templateId("tIDrdFcqFGMsTnc462H49_DbjgXUuIjsqIlQttq7VDE")
                .data(wxMpTemplateData)
                .url("http://www.baidu.com")
                .build();
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
        }

    }
}
